<template>
    <div class="wrapper">
        <top-header title="订单管理"></top-header>
        <list class="item-container">
            <refresh @refresh="loadingDown()"></refresh>
            <cell class="border-cell"></cell>
            <cell v-for="(order,key) in orderList"
                class="cell"
                :key="key">
                <div class="content">
                    <div class="content_third">
                        <text class="content_four_text">NO:{{order.id}}</text>
                        <!-- <text class="iconfont content_four_text"></text> -->
                        <text class="iconfont content_four_phone" @click="callPhone(order.userPhone)">&#xe60b;联系客户</text>
                    </div>
                    <text class="content_first blue">{{order.province}} {{order.city}}</text>
                    <text class="content_first">{{order.carname}}</text>
                    <text class="content_first">顾客姓名：{{order.ownername}}</text>
                    <text class="content_first">服务类型：{{order.serviceType}}</text>
                    <div class="content_third">
                        <text class="content_first">服务时间：{{order.appointmentdate}} {{order.appointmenttime}}</text>
                        <text class="order_status blue">{{order.orderStatus}}</text>
                    </div>
                    <div class="commodity" v-for="(demo,key) in order.commodList" :key="key">
                        <image class="content_img" v-if="demo.pics" :src="demo.pics"></image>
                        <image class="content_img" v-else src="bmlocal://assets/acw/car.jpg"></image>
                        <div>
                            <text class="content_first">{{demo.goodsName}}</text>
                            <text class="content_discription">{{demo.description}}</text>
                            <div class="content_third" >
                                <text class="content_four_text">￥：</text>
                                <text class="content_four_text">{{demo.price}}</text>
                            </div>
                        </div>
                    </div>
                    <text class="content_total blue" v-if="order.p1 != 3">总额：{{order.actualAmount}}元</text>
                    <div class="button_right">
                        <text class="content_button" v-if="order.status == 4 && order.p1 != 3" @click="delOrder(order.id)">删除</text>
                        <text class="content_button" v-if="order.status == 2" @click="takeOrder(order.id, 3)">确认订单</text>
                        <text class="content_button" v-if="order.status == 3 && order.p1 == 3" @click="submit(order.id)">提交审核</text>
                        <text class="content_button" v-if="order.status == 6 && order.p1 != 3" @click="takeOrder(order.id, 7)">确认取消</text>
                    </div>
                </div>
            </cell>
            <cell class="cell" v-if="orderList.length <= 0">
                <div class="no-order">
                    <text>暂无订单</text>
                </div>
            </cell>
            <loading class="loading" @loading="onloading(index)" :display="showLoading">
                <loading-indicator class="comm-indicator" v-if="!lastPage"></loading-indicator>
                <text class="comm-indicator-text" >{{loadingText}}</text>
            </loading>
        </list>
    </div>
</template>
<script>
import { WxcButton } from 'weex-ui';
import header from '../../common/header'
export default {
    components: { WxcButton, 
        'top-header': header, 
    },
    data () {
        return {
            token: '',
            orderList: [],
            nextClick: true,
            showLoading: 'hide',
            searchParam: {
                "limit": 3,
                "order": "desc",
                "page": 1,
                "sort": "id",
                "p1": '',
                "storeid": '',
                "uid": '',
            },
            lastPage:false,//是否是最后一页
            loadingText:'加载中 ...',
        }
    },
    created () {
        let StoreInfo = this.$storage.getSync('StoreInfo')
        let user = this.$storage.getSync('UserInfo')
        this.token = user.token
        this.searchParam.storeid = StoreInfo.id
        this.$router.getParams().then(resData => {
            this.searchParam.p1 = resData
            this.loadingDown()
        })
    },
    eros: {
        beforeBackAppear() {
            this.loadingDown()
        }
    },
    methods: {
        // 返回
        back () {
            this.$router.back();
        },
        callPhone (phone) {
            this.$coms.call(phone)
        },
        loadingDown() {
            this.orderList = []
            this.searchParam.page = 1;
            this.getOrderList(this.searchParam)
        },
        onloading() {
           this.searchParam.page = this.searchParam.page + 1;

            this.$notice.loading.show();
            this.showLoading = 'show';
            if(!this.lastPage) {
                this.getOrderList(this.searchParam)
            } 
            setTimeout(() => {
                this.$notice.loading.hide();
                this.showLoading = 'hide'
            }, 600)
        },
        getOrderDetail (list, total) {
            let param = {
                "limit": 200,
                "order": "desc",
                "page": 1,
                "sort": "id",
                "id": list.id,
            }
            let vm = this;
            this.$fetch({
                method: 'POST',    // 大写
                name: 'maintenance.listDetails', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: param
            }).then(resData => {
                // 成功回调
                list.commodList = []
                if(resData && resData.errno == 0){
                    if (resData.data.items[0].orderGoodsEntity) {
                        let items = resData.data.items[0].orderGoodsItemsEntity
                        for (let i = 0, len = items.length; i < len; i++) {
                            items[i].pics = items[i].pics.split(';')[0]
                        }
                        list.commodList = items
                        list.actualAmount = resData.data.items[0].orderGoodsEntity.actualAmount
                    } else {
                        list.actualAmount = 0.00
                    }
                } else {
                    list.actualAmount = 0.00
                }
                this.orderList.push(list)
                if(this.orderList.length >= total){
                    this.lastPage = true;
                    this.loadingText = '没有更多'
                }
            }, error => {
                // 错误回调
                setTimeout(() => {
                    vm.showLoading = 'hide'
                }, 500)
                console.log(error)
            })
        },
        // 查询订单列表
        getOrderList (param) {
            // this.$notice.toast({message: param})
            this.$notice.loading.show();
            this.$fetch({
                method: 'POST',    // 大写
                name: 'maintenance.list', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: param
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    let list = resData.data.items
                    for (let i = 0, len = list.length; i < len; i++) {
                        if (list[i].p1 == 1) {
                            list[i].serviceType = list[i].type == 1? "保养" : "维修"
                        } else if (list[i].p1 == 2){
                            list[i].serviceType = list[i].type == 1? "美容" : "洗车"
                        } else if (list[i].p1 == 3){
                            list[i].serviceType = "车辆检测"
                        }
                        if (list[i].p1 == 3){
                            list[i].orderStatus = this.getCarStatus(list[i].status)
                        } else {
                            list[i].orderStatus = this.getOrderStatus(list[i].status)
                        }
                        this.getOrderDetail(list[i], resData.data.total)
                    }
                   
                    this.$notice.loading.hide();
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 订单状态
        getOrderStatus (num) {
            // status 1 待支付 2 待确认 3 待服务 4 完成
            switch (num) {
                case 1:
                    return '待支付'
                    break
                case 2:
                    return '待确认'
                    break
                case 3:
                    return '待服务'
                    break
                case 4:
                    return '完成'
                    break
            }
        },
        // 车检状态
        getCarStatus (num) {
            // status 0 未审核 1 审核通过 2 审核未通过
            switch (num) {
                case 2:
                    return '待确认'
                    break
                case 3:
                    return '待服务'
                    break
                case 4:
                    return '待审核'
                    break
                case 5:
                    return '完成'
                    break
            }
        },
        // 确认订单
        takeOrder (id, status) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'maintenance.updateStatus', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'id': id, 'status': status}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    this.$notice.toast({message: '确认订单成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({message: '确认订单失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 提交审核
        submit (id) {
            this.$router.open({
                name: 'acw.my.merchant.submitreview',
                type: 'PUSH',
                params: {id: id}
            })
        },
        // 删除订单
        delOrder (id) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'maintenance.delete', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {id: id}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    this.$notice.toast({message: '删除成功'})
                } else {
                    this.$notice.toast({message: '删除失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        }
    }
}
</script>
<style scoped>
@import 'src/js/css/base.scss';
.iconfont {
    font-family: iconfont;
}
.wrapper{
    background-color: #fff;
}
.item-container {
    width: 750px;
    background-color: #fff;
    padding: 20px;
}
.cell{
    background-color: #f2f3f4;
    width: 710px;
}
.border-cell {
    background-color: #f2f3f4;
    width: 750px;
    height: 24px;
    align-items: center;
    justify-content: center;
    border-bottom-width: 1px;
    border-style: solid;
    border-color: #e0e0e0;
}
.no-order {
    justify-content: center;
    align-items: center;
    height: 1034px;
}
.content{
    background-color: #f2f3f4;
    padding: 10px;
    margin-bottom: 20px;
    border-radius: 10px;
    border-bottom-color: #ccc;
    border-bottom-style: solid;
    border-bottom-width: 1px;
}
.commodity {
    flex-direction: row;
    border-style: solid;
    border-bottom-width:1px;
    border-color: #e0e0e0;
    background-color: #fff;
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 10px;
}
.content_img{
    width: 140px;
    height: 140px;
    margin-right: 10px;
}
.content_first, 
.content_total,
.content_four_text{
    font-size: 30px;
    height: 50px;
    line-height: 50px;
    color: #666;
}
.blue{
    color: #1E90FF;
}
.content_total{
    text-align: right;
}
.content_discription{
    font-size: 30px;
}

.content_third{
    flex-direction: row;
}
.content_four_phone{
    height: 50px;
    line-height: 50px;
    margin-left: 380px;
    color: #1da1f2;
    font-size: 30px;
}
.order_status{
    height: 50px;
    line-height: 50px;
    color: #666;
    margin-left: 200px;
}
.button_right{
    align-items: flex-end;
}
.content_button{
    border-style: solid;
    border-width: 1px;
    border-color: #666;
    border-radius: 10px;
    text-align: center;
    margin-top: 10px;
    padding: 20px;
}
</style>
